import InfoBarList from '../InfoBarList/InfoBarList';

function flyTo(map, coordinate) {
    map.flyTo({
        center: coordinate,
        zoom: 15
    });
}

export default class CongestionHandler {
    constructor() {
        this.chart = null;
        this.infoObjArr = [];
        this.num = 0;
    }

    loadData(map, isInitChart) {
        let that = this;
        // minemap.util.getJSON('../data/congestion.json', function (error, data) {
        //     const infoObj = data ? data || {} : {};
        //     that.infoObj = infoObj;
        //     if (isInitChart) {
        //         that.updateChart();
        //     }
        // });

        minemap.util.getJSON('../data/trafficindex_top10.json', function (error, data) {
            data = data ? data : {};
            for (let i = 0; i < 24; i++) {
                const infoObj = {
                    "color1": "#FF5500",
                    "color2": "#EDCF6D",
                    "array": [],
                    callback: flyTo.bind(null, map)
                };
                data[i].features.map((item) => {
                    infoObj.array.push({
                        "name": item.properties.road_name,
                        "sub": item.properties.segment_name,
                        "value": item.properties.trafficindex,
                        "coordinate": item.geometry.coordinates[Math.ceil(item.geometry.coordinates.length / 2)]
                    })
                });
                that.infoObjArr.push(infoObj);
            }
            if (isInitChart) {
                that.updateChart();
            }
        });
    }

    updateChart() {
        this.chart = new InfoBarList('congestion-chart', this.infoObjArr[this.num] || {});
    }

    getInfoObj() {
        return this.infoObj;
    }

    getChart() {
        return this.chart;
    }

    setValueNum(map, valueNum) {
        if (valueNum === 24) valueNum = 0;
        this.num = valueNum;
        this.updateChart()
    }
};